//
//  BaseTabBarVC.m
//  HuiYun
//
//  Created by 刘锦锋 on 16/6/8.
//  Copyright © 2016年 hpx. All rights reserved.
//

#import "BaseTabBarVC.h"
#import "DWTabBar.h"
#import "ThirdMineViewController.h"
#import "SecondMsgViewController.h"
#import "FirstSearchViewController.h"


@interface BaseTabBarVC ()

@end

@implementation BaseTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 设置 TabBarItemTestAttributes 的颜色。
    [self setUpTabBarItemTextAttributes];
    
    // 设置子控制器
    [self setUpChildViewController];
    
    // 处理tabBar，使用自定义 tabBar 添加 发布按钮
    [self setUpTabBar];
    
    [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
    
    //去除 TabBar 自带的顶部阴影
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    
    self.tabBar.tintColor=[UIColor clearColor];
    
    //设置导航控制器颜色为黄色
    [[UINavigationBar appearance] setBackgroundImage:[self imageWithColor:RGB(245, 245, 247)] forBarMetrics:UIBarMetricsDefault];
}

/**
 *  利用 KVC 把 系统的 tabBar 类型改为自定义类型。
 */
- (void)setUpTabBar{
    
    [self setValue:[[DWTabBar alloc] init] forKey:@"tabBar"];
}




/**
 *  tabBarItem 的选中和不选中文字属性
 */
- (void)setUpTabBarItemTextAttributes{
    
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor clearColor];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor clearColor];
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateHighlighted];
    
}

/**
 *  添加子控制器，我这里值添加了4个，没有占位自控制器
 */
- (void)setUpChildViewController{
  FirstSearchViewController   *FirstVC = [[UIStoryboard storyboardWithName:@"FirstSearchViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"FirstSearchViewController"];
    SecondMsgViewController   *SecondVC = [[UIStoryboard storyboardWithName:@"SecondMsgViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"SecondMsgViewController"];
    ThirdMineViewController   *ThirdVC = [[UIStoryboard storyboardWithName:@"ThirdMineViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ThirdMineViewController"];
    
    
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:FirstVC]
                                                                                    WithTitle:@"你来约"
                                                                                    imageName:@"FirstTab_unSelect"
                                                                            selectedImageName:@"FirstTab_Select"];
    
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:SecondVC]
                          WithTitle:@"信息"
                          imageName:@"msg_unSelect"
                  selectedImageName:@"msg_Select"];
    
    
    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:ThirdVC]
                          WithTitle:@"我的"
                          imageName:@"Mine_unSelect"
                  selectedImageName:@"Mine_Select"];
    
    
//    [self addOneChildViewController:[[UINavigationController alloc]initWithRootViewController:[[DWMineViewController alloc]init]]
//                          WithTitle:@"我的"
//                          imageName:@"account_normal"
//                  selectedImageName:@"account_highlight"];
    
}

/**
 *  添加一个子控制器
 *
 *  @param viewController    控制器
 *  @param title             标题
 *  @param imageName         图片
 *  @param selectedImageName 选中图片
 */

- (void)addOneChildViewController:(UIViewController *)viewController WithTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    viewController.view.backgroundColor     = RGB(255, 255,255);
    viewController.tabBarItem.title         = title;
    viewController.tabBarItem.image         = [UIImage imageNamed:imageName];
    UIImage *image = [UIImage imageNamed:selectedImageName];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = image;
    [self addChildViewController:viewController];
    
}
//这个方法可以抽取到 UIImage 的分类中
- (UIImage *)imageWithColor:(UIColor *)color
{
    NSParameterAssert(color != nil);
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // Create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
